<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Summary extends CI_Controller {

	public function index()
	{	
		 //welcome page
		 $d['hello'] ="data";		 
		 $views['_content'] = "ordersummary";
		 $this->template->load('template_landing', $views, $d);
		 
		 $this->load->model('transaction');	 

		 $fName = $this->input->post("Fname"); 
	     $mName = $this->input->post("Mname");
		 $lName = $this->input->post("Lname");
		 $nName = $this->input->post("Nname");
		 $email = $this->input->post("Eadd");
		 if(isset($_COOKIE['mobtel'])) $msisdn = $_COOKIE['mobtel'];
		 $otherCont = $this->input->post("Othercont");
		 //$birthdate = date("Y-m-d",strtotime($this->input->post("birthday")));
		 if(isset($_COOKIE['bday'])) $birthdate = date("Y-m-d",strtotime($_COOKIE['bday']));
	     $street = $this->input->post("Address");
		 if(isset($_COOKIE['area'])) $area = $_COOKIE['area'];
	     if(isset($_COOKIE['town'])) $town = $_COOKIE['town'];
		 if(isset($_COOKIE['province'])) $province = $_COOKIE['province'];
		 if(isset($_COOKIE['postalcode'])) $postalCode = $_COOKIE['postalcode'];
		 if(isset($_COOKIE['gadgetid'])) $gadgetId = $_COOKIE['gadgetid'];
	     if(isset($_COOKIE['promoid'])) $promoId = $_COOKIE['promoid'];
		 if(isset($_COOKIE['globestoreid'])) $storeId = $_COOKIE['globestoreid'];
		 if(isset($_COOKIE['commitmentMonths'])) $commitmentMonths = $_COOKIE['commitmentMonths'];
		 
	     if(isset($_COOKIE['mode'])){
		    if($_COOKIE['mode']=="edit"){
			   if($fName!="")
		          $this->transaction->saveUserProfileToDB($fName, $mName, $lName, $nName, $email, $msisdn, $otherCont, $birthdate, $street, $area, $town, $province, $postalCode, $gadgetId, $promoId, $commitmentMonths);
			   else 
			      $this->transaction->saveStoreIdToDB($storeId);
			}
			else $this->transaction->saveStoreIdToDB($storeId);
		 }
		 else $this->transaction->saveStoreIdToDB($storeId);
	}

	public function sendPlanCode($subsId = "")
	{	
		// default error
		$return['status'] = -1;
		$return['description'] = "There was an error encountered. Please try again.";
			
		//$mobtel = ($mobtel!="") ? urldecode($mobtel) : "";
		$subsId = ($subsId!="") ? urldecode($subsId) : "";
		
		$this->load->model('transaction');	 
		//$userProfile = $this->transaction->retrieveUserProfileToDB($mobtel);
		$userProfile = $this->transaction->retrieveUserProfileToDB($subsId);
		
		//print_r($userProfile);
		try
		{
			if($userData = $userProfile[0])
			{
				$nName = $userData['nName'];	// not passed in api
				
				//$subsObj = new SubscriberObj();
				$fName = $userData['fName'];
				$mName = $userData['mName'];
				$lName = $userData['lName'];
				$email = $userData['email'];
				//$msisdn = $mobtel;
				$msisdn = $mobtel = ($userData['msisdn']==0) ? "" : $userData['msisdn'];
				
				$birthdate = $userData['birthdate'];
				// cleanup birthdate
				$birthdate = date("m/d/Y", strtotime($birthdate));
				
				$streetNum = $userData['street'];
				$area = (strlen($userData['area']) > 2) ? $userData['area'] : "";
				$town = $userData['town'];
				$province = $userData['province'];
				$postalCode = $userData['postalCode'];
				$gadgetId = $userData['gadgetId'];
				$promoId = $userData['promoId'];
				
				//$dt = new DateTime($userData['commitmentPeriod']);
				//$commitmentPeriod = $dt->format("m/d/Y");
				$commitmentPeriod = $userData['commitmentPeriod'];
				
				$storeId = $userData['storeId'];
				$channelId = "2";
				
				// submit now to API;
				$this->load->library('Maitaidata');
				//$regReturn = $this->maitaidata->registerSubscriber($subsObj);
				$regReturn = $this->maitaidata->registerSubscriber($fName, $mName, $lName, $email, $mobtel, $birthdate, $streetNum, $area, $town, $province, $postalCode, $gadgetId, $promoId, $commitmentPeriod, $storeId, $channelId);
				
				try
				{
					//print_r($regReturn);
					//var_dump($regReturn);
					//die();
					$resp = $regReturn->obj;
					// if true will send 0 print plan code
					//log_message('error', "resp:". $resp);
					//log_message('error', "responseCode:". $resp->responseCode);
					if($resp->responseCode==0)
					{
						$planCode = $resp->planCode;
						//log_message('error', "planCode:". $planCode);
						// save plan code to db
						$update = $this->transaction->updatePlanCode($subsId, $planCode);
						if($update) {
							// return json with plancode
							$return['status'] = 0;
							$return['planCode'] = $resp->planCode;
							$return['name'] = ($nName=="") ? $fName : $nName;
						}
					} else {
						// return error registering
						$return['status'] = -1;
						$return['description'] = $resp->responseDesc;
						//log_message('error', "responseDesc:". $resp->responseDesc);
					}
				//print_r($subsObj);
				} catch (Exception $e) {
					$return['status'] = -1;
					$return['description'] = "There was an error encountered. Please try again.";
				}
			}
		}
		catch (Exception $e) {
			$return['status'] = -1;
			$return['description'] = "There was an error encountered. Please try again.";
		}
		die(json_encode($return));
	}
	
	//public function retrieveUserProfileToDB($mobtel = ""){
	public function retrieveUserProfileToDB($subsId = ""){
		$this->load->model('transaction');
		//$mobtel = ($mobtel!="") ? urldecode($mobtel) : "";
		$subsId = ($subsId!="") ? urldecode($subsId) : "";
		//$userProfile = $this->transaction->retrieveUserProfileToDB($mobtel);
		$userProfile = $this->transaction->retrieveUserProfileToDB($subsId);
		//print_r(json_encode($userProfile));
		print_r($userProfile);
		die();
	}
}